/*
 * Copyright (C) 2004, Velocityme Partnership
 * StateMachineActions.java
 *
 * Created on 19 October 2004, 06:47
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.TaskStateMachineValue;
import com.velocityme.www.actionforms.StateMachineActionForm;
import com.velocityme.www.utility.NodeUtil;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class StateMachineActions extends NodeActions {
    
    /** Creates a new instance of StateMachineActions */
    public StateMachineActions() {
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskStateMachinePK taskStateMachinePK = new TaskStateMachinePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        TaskStateMachineValue taskStateMachineValue = cs.getTaskStateMachineValue(key, taskStateMachinePK);
        session.setAttribute("taskStateMachineValue", taskStateMachineValue);
        return mapping.findForward("ACTION_FORWARD");
    }
    
    public ActionForward showEditForward(ActionMapping mapping, 
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)session.getAttribute("taskStateMachineValue");
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        StateMachineActionForm actionForm = (StateMachineActionForm)form;
        actionForm.setCmd(0,"");
        actionForm.setTransitionValues(taskStateMachineValue.getTransitionValues(), NodeUtil.getAllRolePairs(key, cs));
        session.setAttribute("statePairs", NodeUtil.getAllStatePairs(key, cs));
        session.setAttribute("allButStartStatePairs", NodeUtil.getAllButStartStatePairs(key, cs));
        session.setAttribute("statusPairs", NodeUtil.getAllStatusPairs(key, cs));
        session.setAttribute("rolePairs", NodeUtil.getAllRolePairs(key, cs));
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)session.getAttribute("taskStateMachineValue");
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        StateMachineActionForm actionForm = (StateMachineActionForm)form;
        if (actionForm.processCmd(NodeUtil.getAllRolePairs(key, cs))) {
            actionForm.setCmd(0,"");
            return mapping.findForward("ACTION_EDIT");
        }
        if (request.getParameter("btnSubmit").equals("OK")) {
            TaskStateMachinePK taskStateMachinePK = taskStateMachineValue.getPrimaryKey();
            taskStateMachineValue.setTransitionValues(actionForm.getTransitionValues(cs.getAllStateValues(key), cs.getAllStatusValues(key), cs.getAllRoleValues(key)));
            cs.taskStateMachineEdit(key, taskStateMachinePK, taskStateMachineValue);
        }
        NodePK nodePK = new NodePK(taskStateMachineValue.getTaskStateMachineId());
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
